/* 商品详情页Detail数据管理仓库 */
import { reqGoodsInfo,reqAddOrUpdateShopCart } from "@/api"
import { getUUID } from "@/utils/uuid_token";

//仓库三剑客
const actions = {
   async getGoodsDetails({commit},goodsId){
      let result =  await reqGoodsInfo(goodsId) 
      console.log(result);
      if(result.code === 200){  //200:成功状态码
         commit('SETGOODSDETAILS',result.data)
      }
   },

   //将id和数量发给服务器（并不会接收到数据，只需写actions）
  async addOrUpdateShopCart({commit},{goodsId,goodsNum}){
       let result = await reqAddOrUpdateShopCart(goodsId,goodsNum)  
       if(result.code === 200){
        // 成功
        return 'success'
       }else{
        // 失败
        return Promise.reject(new Error('failure'))
       }
       
   }
}

const mutations = {
    SETGOODSDETAILS(state,value){
        state.goodsDetails = value
    }
}

const state = {
    goodsDetails:{},
    uuid_token:getUUID()        //用户身份标识
}

//getters数据获取方法
const getters = {
  //分类Id
  categoryView(state){
    return state.goodsDetails.categoryView || {}  //防止因为undefined而报错
  },

  //商品信息
  skuInfo(state){
    return state.goodsDetails.skuInfo || {}
  },
  
  //商品属性
  spuSaleAttrList(state){
    return state.goodsDetails.spuSaleAttrList || []
  },

}

export default {
    namespaced:true,
    actions,
    mutations,
    state,
    getters
}